package com.enation.app.shop.component.goodscore.plugin.area;

import com.enation.app.shop.ShopApp;
import com.enation.app.shop.core.goods.model.Cat;
import com.enation.app.shop.core.goods.plugin.AbstractGoodsPlugin;
import com.enation.app.shop.core.goods.plugin.IGoodsTabShowEvent;
import com.enation.app.shop.core.goods.service.IGoodsCatManager;
import com.enation.app.shop.front.policyInsurance.entity.SysArea;
import com.enation.app.shop.front.policyInsurance.service.ISysAreaMapper;
import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
import com.enation.eop.sdk.context.EopSetting;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 保险区域信息插件
 * @author kingapex
 *
 */
@Component
public class InsuranceAreaPlugin extends AbstractGoodsPlugin implements IGoodsTabShowEvent {
    @Autowired
    private ISysAreaMapper sysAreaMapper;
    @Autowired
    private IDaoSupport daoSupport;
    @Autowired
    private IGoodsCatManager goodsCatManager;
    @Override
    public String getAddHtml(HttpServletRequest request) {
        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        //由request中读取所属分类id
        int catid = StringUtil.toInt(request.getParameter("catid"),true);
        List<Cat> cats=this.goodsCatManager.getParents(catid);

        if(cats.get(0).getCat_id()==244||cats.get(0).getCat_id()==231){
            //查出全部省份
            List<SysArea> sysAreasList = sysAreaMapper.getByAreaLevel(1);
            freeMarkerPaser.setPageName("insuranceArea_input");
            freeMarkerPaser.putData("sysAreasList",sysAreasList);
            freeMarkerPaser.putData("optype",request.getParameter("optype")); //操作类型
            if("b2b2c".equals(EopSetting.PRODUCT)){
                freeMarkerPaser.putData("self_store","yes");
                freeMarkerPaser.putData("store_id",ShopApp.self_storeid);
            }
            return freeMarkerPaser.proessPageContent();
        }
        return null;
    }



    @Override
    public String getEditHtml(Map goods, HttpServletRequest request) {


        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        freeMarkerPaser.putData("goodsView",goods);
        freeMarkerPaser.putData("optype",request.getParameter("optype"));
        freeMarkerPaser.setPageName("goods_edit");
        if("b2b2c".equals(EopSetting.PRODUCT)){
            freeMarkerPaser.putData("self_store","yes");
            freeMarkerPaser.putData("store_id",ShopApp.self_storeid);
        }
        return freeMarkerPaser.proessPageContent();

    }


    @Override
    public void onBeforeGoodsEdit(Map goods, HttpServletRequest request) {


    }




    @Override
    public void onBeforeGoodsAdd(Map goods, HttpServletRequest request) {

    }



    @Override
    public void onAfterGoodsAdd(Map goods, HttpServletRequest request)
            throws RuntimeException {


    }

    @Override
    public void onAfterGoodsEdit(Map goods, HttpServletRequest request) {
        this.daoSupport.execute("update es_cart set is_change = 1 where goods_id = ?", goods.get("goods_id"));
    }





    @Override
    public String getTabName(Integer typeid) {
        return "保险投放区域";
    }


    @Override
    public int getOrder() {
        return 10;
    }



}
